// pages/my/my.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0,
    mybackground: null,
    userInfo: {
      code: ''
    },
    headimgurl: null,
    weixinnickname: null,
    buttonhidden: true,
    flag1: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var height = that.data.height
    var buttonhidden = that.data.buttonhidden
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          height: res.screenHeight
        })
      },
    })






  },
  myorder: function() {
    wx.navigateTo({
      url: '../../pages/myorder/myorder',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.login({
      success(res) {
        // that.data.userInfo.code = res.code;
        console.log(res.code);
        that.setData({
          userInfo: {
            code: res.code
          }
        })
      }
    })
    var that = this
    var flag1 = that.data.flag1
    var weixinnickname = that.data.weixinnickname
    var headimgurl = that.data.headimgurl

    var weixinnickname1 = wx.getStorageSync("weixinnickname")
    var headimgurl1 = wx.getStorageSync("headimgurl")
    if (weixinnickname1 == "" || headimgurl1 == "") {

      that.setData({
        flag1: false
      })
      // that.access()

    } else {

      that.setData({
        headimgurl: headimgurl1,
        weixinnickname: weixinnickname1,
        flag1: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  access(res) {
    console.log(res)
    var userInfo = res.detail,
      that = this;
    var flag1 = that.data.flag1
    wx.request({
      url: app.globalData.url +"/wechat/decodeUserInfo", //仅为示例，并非真实的接口地址
      data: {
        encryptedData: userInfo.encryptedData,
        iv: userInfo.iv,
        code: that.data.userInfo.code,

      },
      success(res) {
        console.log(res)
        that.data.userInfo = res.data.userInfo;
        var openid = res.data.userInfo.openid
        wx.setStorageSync("openid", openid)
        var weixinnickname = res.data.userInfo.weixinnickname
        var headimgurl = res.data.userInfo.headimgurl
        wx.setStorageSync("weixinnickname", weixinnickname)
        wx.setStorageSync("headimgurl", headimgurl)
        wx.setStorageSync("userid", res.data.userInfo.id)
        app.globalData.userInfo = res.data.userInfo
        that.setData({
          headimgurl: headimgurl,
          weixinnickname: weixinnickname,
          flag1: true
        })
      }
    })
  },
})